package com.xiamo.job;

import com.xiamo.entity.Message;
import com.xiamo.service.IMessageService;
import com.xiamo.utils.ws.SimpleWebsocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MessageScheduler {

    @Autowired
    private SimpleWebsocketServer simpleWebsocketServer;

    @Autowired
    private IMessageService messageService;

    @Scheduled(fixedRate = 5000000) // 每隔5秒执行一次
    public void sendMessagesToAll() {
        List<Message> messages = messageService.list(); // 获取留言列表
        simpleWebsocketServer.sendMessagesToAll(messages); // 实时发送留言信息给所有在线客户端
    }
}
